package com.zb.jsp.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.zb.jsp.common.HibernateSessionFactory;
import com.zb.jsp.pojo.TblAdvertAddress;
import com.zb.jsp.pojo.TblManager;
import com.zb.jsp.utils.PropertiesCopyUtil;
import com.zb.jsp.utils.StringUtil;
import com.zb.jsp.vo.AdvertAddressVo;

public class TblAdvertAddressDao {

	Session session = null;
	Transaction transaction = null;

	/**
	 * 向数据表advertaddress插入信息
	 * 
	 * @param tblAdvertAddress
	 *            返回一个TblAdvertAddress对象
	 */
	public void addTblAdvertAddress(TblAdvertAddress tblAdvertAddress) {

		try {
			// step 1 : 创建一个SessionFactory
			session = HibernateSessionFactory.getSession();

			// step 2 : 打开一个事务
			transaction = session.beginTransaction();

			// step 4 : 准备数据
			/*
			 * TblManager talmanager = new TblManager();
			 * talmanager.setEmail("zz@123.com"); talmanager.setAddress("dfa");
			 * talmanager.setPasswd("d"); talmanager.setTel("23423424");
			 * talmanager.setUserCn("哦撒的红发");
			 */

			// step 3 :调用 session接口进行查询操作
			session.save(tblAdvertAddress);
			// step 4 : 提交事务
			transaction.commit();
		} catch (Exception e) {
			// step 异常：事务回滚
			if (transaction != null) {
				transaction.rollback();
			}
			e.printStackTrace();
		} finally {
			// step 5 : 关闭session
			HibernateSessionFactory.closeSession();
		}
	}

	/**
	 * 根据hql文查询
	 * 
	 * @param hql
	 *            hql文
	 * @return
	 */
	public List<TblAdvertAddress> fidTblAdvertAddressListByHql(String hql) {

		List<TblAdvertAddress> tblAdvertAddressList = null;
		try {
			// 1.创建 Session链接
			session = HibernateSessionFactory.getSession();

			// 2.获得一个查询的query
			Query hqlQuery = session.createQuery(hql);
			// 3.执行查询动作
			tblAdvertAddressList = hqlQuery.list();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 4.关闭session
			HibernateSessionFactory.closeSession();
		}
		return tblAdvertAddressList;
	}

	/**
	 * 根据广告位类型查询广告位信息列表
	 * 
	 * @param advertAddressVo
	 *            从页面传来的vo对象
	 * @return 广告位信息列表
	 */
	public List<AdvertAddressVo> findAdvertAddressVoListByAdvertAddressVoQry(
			AdvertAddressVo advertAddressVoQry) {
		List<AdvertAddressVo>

		advertAddressVoList = null;
		Session session = null;

		try {
			// 获取一个 Session
			session = HibernateSessionFactory.getSession();

			// 定义HQL文
			// com.insigma.gongda.vo.AdvertAddressVo advertAddressVo = new
			// com.insigma.gongda.vo.AdvertAddressVo(advertName,width,notes);
			String hql = "select new com.zb.jsp.vo.AdvertAddressVo(taa.id,taa.advertName,taa.width,taa.height,taa.notes,tdf.value,taa.defPath) from TblAdvertAddress taa,TblDefType tdf where taa.defType=tdf.code ";
			// 判断页面上是否输入了广告位名的查询条件
			if (!StringUtil.isNullOrEmpty(advertAddressVoQry.getAdvertName())) {
				hql = hql + " and taa.advertName like '%"
						+ advertAddressVoQry.getAdvertName() + "%'";
			}
			hql = hql + " and taa.defType = '"
					+ advertAddressVoQry.getDefType() + "'";

			// 获得一个查询的query
			Query hqlQuery = session.createQuery(hql);

			// 计算从第几条记录开始取数据(（【当前是第几页】-1）*【每一页显示多少条记录】)
			int firstRow = (advertAddressVoQry.getCurPage() - 1)
					* advertAddressVoQry.getNumPerPage();

			// 游标从这个点位开始取数据
			hqlQuery.setFirstResult(firstRow);
			// 每页取多少数据 10条
			hqlQuery.setMaxResults(advertAddressVoQry.getNumPerPage());

			// 执行查询动作
			advertAddressVoList = hqlQuery.list();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭session
			HibernateSessionFactory.closeSession();
		}
		return advertAddressVoList;
	}

	public int findAdvertAddressVoCountByAdvertAddressVoQry(
			AdvertAddressVo advertAddressVoQry) {

		Session session = null;
		int advertAddressVoCount = 0;

		try {
			// 获取一个 Session
			session = HibernateSessionFactory.getSession();

			// 定义HQL文
			// com.insigma.gongda.vo.AdvertAddressVo advertAddressVo = new
			// com.insigma.gongda.vo.AdvertAddressVo(advertName,width,notes);
			String hql = "select count(*) from TblAdvertAddress taa,TblDefType tdf where taa.defType=tdf.code ";
			// 判断页面上是否输入了广告位名的查询条件
			if (!StringUtil.isNullOrEmpty(advertAddressVoQry.getAdvertName())) {
				hql = hql + " and taa.advertName like '%"
						+ advertAddressVoQry.getAdvertName() + "%'";
			}
			hql = hql + " and taa.defType = '"
					+ advertAddressVoQry.getDefType() + "'";

			// 获得一个查询的query
			Query hqlQuery = session.createQuery(hql);

			// 执行查询动作
			List queryResult = hqlQuery.list();
			// 获取记录总数
			advertAddressVoCount = ((Long) queryResult.get(0)).intValue();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭session
			HibernateSessionFactory.closeSession();
		}
		return advertAddressVoCount;
	}

	/**
	 * 根据传入的id序列删除数据库信息
	 * 
	 * @param ids
	 *            id序列拼接的字串
	 */
	public void delAdvertAddress(String ids) {
		Session session = null;
		Transaction tc = null;
		try {
			// 获取一个 Session连接
			session = HibernateSessionFactory.getSession();

			// 打开一个事务
			tc = session.beginTransaction();

			// ids为要删除的信息的id的拼接
			String hql = "delete from TblAdvertAddress where id in (" + ids
					+ ")";

			// 调用 session接口进行删除操作
			Query hqlQuery = session.createQuery(hql);

			hqlQuery.executeUpdate();

			// 提交事务
			tc.commit();
		} catch (Exception e) {
			if (tc != null) {
				// 事务回滚
				tc.rollback();
			}
			e.printStackTrace();
		} finally {
			// 关闭session
			HibernateSessionFactory.closeSession();
		}
	}

	/**
	 * 根据id查询数据库信息
	 * 
	 * @param id
	 *            传入的id号
	 * @return 返回给 TblAdvertAddress 对象
	 */
	public TblAdvertAddress findTblAdvertAddressById(int id) {
		// 用来存放查询结果的pojo对象
		TblAdvertAddress tblAdvertAddress = null;

		try {
			// step 1 : 获取一个 Session连接
			Session session = HibernateSessionFactory.getSession();

			// step 2 :调用 session接口进行根据主键查询操作
			tblAdvertAddress = (TblAdvertAddress) session.get(
					TblAdvertAddress.class, id);
		} finally {
			// step 3 : 关闭session
			HibernateSessionFactory.closeSession();
		}

		return tblAdvertAddress;

	}

	/**
	 * 更新广告位数据到tbl_advert_address
	 * 
	 * @param tblAdvertAddress
	 *            广告位信息
	 */
	public void uptTblAdvertAddress(AdvertAddressVo advertAddressVo) {
		Session session = null;
		Transaction tc = null;
		try {
			// step 1 : 获取一个 Session连接
			session = HibernateSessionFactory.getSession();

			// step 2 : 打开一个事务
			tc = session.beginTransaction();

			// 取得数据库里原来的数据
			TblAdvertAddress tblAdvertAddressDb = (TblAdvertAddress) session
					.get(TblAdvertAddress.class, advertAddressVo.getId());
			// 保存文件上传路径
			String defPath = tblAdvertAddressDb.getDefPath();
			// 将vo中 的信息复制到pojo中去
			PropertiesCopyUtil.copy(tblAdvertAddressDb, advertAddressVo);

			// 假如页面上输入的默认文件路径为空
			if (StringUtil.isNullOrEmpty(tblAdvertAddressDb.getDefPath())) {
				// 把原来的文件路径设置到更新对象中
				tblAdvertAddressDb.setDefPath(defPath);
			}

			// step 3 :调用 session接口进行增加操作
			session.update(tblAdvertAddressDb);

			// step 4 : 提交事务
			tc.commit();
		} catch (Exception e) {
			if (tc != null) {
				// other:事务回滚
				tc.rollback();
			}
			e.printStackTrace();
		} finally {
			// step 5 : 关闭session
			HibernateSessionFactory.closeSession();
			System.out.println("广告位信息修改成功");
		}
	}
}